package com.wxmit.module.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wxmit.base.util.DateUtils;
import com.wxmit.base.util.StringUtils;
import com.wxmit.system.base.core.ShushanServiceImpl;
import com.wxmit.module.system.domain.SysDept;
import com.wxmit.module.system.mapper.SysDeptMapper;
import com.wxmit.module.system.service.ISysDeptService;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Description SysDeptServiceImpl
 * @Author huangxiong
 * @Date 2022/7/11 14:49
 * @Version V1.0
 */
@Service
public class SysDeptServiceImpl extends ShushanServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Override
    public List<Tree<Long>> listTree() {
        List<SysDept> list = list();

        return buildDeptTree(list);
    }

    @Override
    public boolean save(SysDept entity) {
        checkNameUnion(entity);
        refreshAncestors(entity);

        return super.save(entity);
    }

    @Override
    public boolean updateById(SysDept entity) {
        checkNameUnion(entity);
        refreshAncestors(entity);
        return super.updateById(entity);
    }

    private void checkNameUnion(SysDept entity){
        LambdaQueryChainWrapper<SysDept> lambdaQuery = lambdaQuery();
        lambdaQuery.eq(SysDept::getName,entity.getName());
        if(Objects.nonNull(entity.getId())){
            lambdaQuery.ne(SysDept::getId,entity.getId());
        }
        Long count = lambdaQuery.count();
        Assert.isTrue(count.compareTo(0L) == 0,"当前部门名已被占用！");
    }

    /**
     *  刷新祖先
     * @param entity 更新对象
     */
    private void refreshAncestors(SysDept entity){
        if(Objects.nonNull(entity.getParentId()) && entity.getParentId() != 0L){
            SysDept pDept = getById(entity.getParentId());
            entity.setAncestors(StringUtils.format("{},{}",pDept.getAncestors(),pDept.getId()));
        }else{
            entity.setAncestors(StringUtils.EMPTY);
        }
    }

    private List<Tree<Long>> buildDeptTree(List<SysDept> list){
        if(CollectionUtil.isEmpty(list))return Collections.emptyList();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        List<Tree<Long>> treeList = TreeUtil.build(list, 0L, treeNodeConfig, (it, tree) -> {
            tree.setId(it.getId()).setName(it.getName()).setWeight(it.getWeight()).setParentId(it.getParentId());
            tree.put("ancestors", it.getAncestors());
            tree.put("leader", it.getLeader());
            tree.put("phone", it.getPhone());
            tree.put("email", it.getEmail());
            tree.put("status", it.getStatus());
            tree.put("parentName", it.getParentName());
            tree.put("createBy", it.getCreateBy());
            tree.put("createTime", DateUtils.format(it.getCreateTime(),DateUtils.YYYY_MM_DD));
            tree.put("updateBy", it.getUpdateBy());
            tree.put("updateTime", DateUtils.format(it.getUpdateTime(),DateUtils.YYYY_MM_DD));
        });
        return treeList;
    }

}
